# coding:utf-8

import requests
from bs4 import BeautifulSoup


class YunDa:

    def __init__(self, start_pro, start_city, des_pro, des_city):
        self.province_city_code = {
            u"广东": 440000,
            u"深圳": 440300,
            u"浙江": 330000,
            u"金华": 330700,
            u"青海": 630000,
            u"西宁": 630100,
            u"西藏": 540000,
            u'拉萨': 540100,
            u"上海": 310000,
            u"北京": 110000
        }
        self.data = {
            "act": "Getyunfei",
            "sp": self.province_city_code[start_pro],
            "ss": self.province_city_code[start_city],
            "mp": self.province_city_code[des_pro],
            "ms": self.province_city_code[des_city],
            "zl": 1
        }
        # self.data = {
        #   "iniweights": 3,
        #   "weight": 3,
        #   "selPro": u"广东",
        #   "selCity": u"深圳",
        #   "selDis": None,
        #   "desPro": u"浙江",
        #   "desCity": u"金华",
        #   "desDis": None
        # }

        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
        }

    def get_price(self):
        self.data["zl"] = 1
        r = requests.post('http://www.yundaex.com/cn/data/search.php', data=self.data, headers=self.headers)
        first_price = int(float(r.text))
        self.data["zl"] = 2
        r = requests.post('http://www.yundaex.com/cn/data/search.php', data=self.data, headers=self.headers)
        second_price = int(float(r.text))
        return [first_price, second_price - first_price]
